{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fe518f3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87442 120036 427.62610766083765 Hz\n",
      "['88150.77678', '0', '0.8280059462620806', '0.5314327126985153', '0.39750946211500204', '88150.77678-0', '28.369752901573236', '52.033134560158175', '705.1211159195539', '20.0', '15.0', '-20.0', '0.01152676874215287', '0.051001789765111646', '0.9986320398640939', '', '', '', '', '', '']\n",
      "[88150.78678, '0', '0.5502584923810523', '0.33792335450596644', '0.17330059543683884', '88150.779759-1', '-141.8791689729382', '160.8677791527232', '683.8931076235057', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14039693931103547', '0.2008910130307643', '0.9695006448247298', '', '', '', '', '', '']\n",
      "[88150.79678, '1', '0.7063552247599557', '0.49397474725559637', '0.4084287570104117', '88150.79569-0 88150.795675-1', '2.226757768794034', '-19.036159854780287', '-288.72722534107356', '20.0', '15.0', '-20.0', '0.06514516047431963', '0.027374145920454677', '0.9975002577452808', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1530847520875171', '0.2209875356663993', '0.9631871924804625']\n",
      "[88150.80678, '1', '0.7910322078633543', '0.49472831656855015', '0.40514624513285424', '88150.804471-0 88150.80479-1', '1.9446636659802365', '-19.682136143449362', '-288.19418100551803', '20.0', '15.0', '-20.0', '0.06623886767176704', '0.023517941823706083', '0.9975266005585715', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15222844217062562', '0.22981420200416583', '0.9612553947580701']\n",
      "[88150.81678, '2', '0.8361660611207393', '0.49362903233597083', '0.40058294061730537', '88150.81599799999-0 88150.815997-1', '2.3733773877048456', '-20.832665442340023', '-291.0391610701988', '20.0', '15.0', '-20.0', '0.06396210553253093', '0.024893336791321347', '0.9976418048775022', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15209387168484892', '0.23398036211890086', '0.960271130638957']\n",
      "[88150.82678, '2', '0.9151973420751922', '0.4936116815701956', '0.39895021008258913', '88150.827729-0 88150.823548-1', '2.4128100758327182', '-21.468145193186366', '-295.0710570327717', '20.0', '15.0', '-20.0', '0.06288346587545898', '0.02509974236087054', '0.9977052032804625', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15004148584820762', '0.23453606423953438', '0.9604584254903964']\n",
      "[88150.83678, '2', '0.8981226615721961', '0.49311319524399827', '0.3989479733166602', '88150.83570099999-0 88150.83570099999-1', '2.580643834051319', '-21.2999141960879', '-292.7523092034187', '20.0', '15.0', '-20.0', '0.06281947395024938', '0.027157390027912388', '0.9976553462290907', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15188909468215658', '0.23346737957802458', '0.9604283864971951']\n",
      "[88150.84678, '2', '0.8762689020545384', '0.49287244828606297', '0.39792630733730605', '88150.843448-0 88150.84344899999-1', '2.70037871965768', '-21.753074785467472', '-295.9881540539876', '20.0', '15.0', '-20.0', '0.0616606383371786', '0.027373421145385487', '0.9977217355027653', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1505621238051983', '0.23344803725625876', '0.960642004482663']\n",
      "[88150.85678, '3', '0.8028764861728308', '0.4927015882674469', '0.39856962598805834', '88150.85654-0 88150.85654-1', '2.766044192974574', '-21.623266864181776', '-296.08798529724027', '20.0', '15.0', '-20.0', '0.06140321642039546', '0.025916912799037132', '0.9977765073623442', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15073019651578715', '0.2338791905465301', '0.960510766252837']\n",
      "[88150.86678, '3', '0.922197307158175', '0.4925920221557682', '0.39870466654079517', '88150.867514-0 88150.86451599999-1', '2.8279107518092026', '-21.750937954882396', '-298.2332454694398', '20.0', '15.0', '-20.0', '0.06071702183202721', '0.025219893820897885', '0.9978363594375146', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14981428045739542', '0.2335102349998405', '0.9607438011880963']\n",
      "[88150.87678, '3', '0.7797779563318543', '0.4920490799623768', '0.3986402655486505', '88150.87172699999-0 88150.87545699999-1', '3.0366961074587433', '-21.77569063112226', '-298.38293212936594', '20.0', '15.0', '-20.0', '0.05994958006002151', '0.025324603360997427', '0.997880109189093', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1504509230615827', '0.2334355900843485', '0.9606624511408207']\n",
      "[88150.88678, '4', '0.9336578810932825', '0.49216591828673045', '0.3991673717146227', '88150.88389-0 88150.88827099999-1', '2.993379604392894', '-21.671916187220244', '-298.5133295164393', '20.0', '15.0', '-20.0', '0.06007445660943474', '0.024427210448378187', '0.997894969950642', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1502346770715741', '0.23348674078640194', '0.9606838625074033']\n",
      "[88150.89678, '4', '0.8550647363113325', '0.49196301857445757', '0.3980274149338666', '88150.89551599999-0 88150.891702-1', '3.1042410838204244', '-22.154830596727386', '-301.7536334011173', '20.0', '15.0', '-20.0', '0.05899829367017455', '0.024598649249555092', '0.9979549628110009', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14891582435890977', '0.23373323204977325', '0.9608292530367055']\n",
      "[88150.90678, '4', '0.860093311910097', '0.49155932443364775', '0.3981537095713097', '88150.90741599999-0 88150.90357499999-1', '3.2627763654987465', '-22.145110012300265', '-301.9952627615938', '20.0', '15.0', '-20.0', '0.05839478901870607', '0.02400047627187978', '0.998005022910298', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14932225425095516', '0.23403121968543314', '0.9606936309760579']\n",
      "[88150.91678, '5', '0.9291327620219251', '0.4914388450903873', '0.39748146803382634', '88150.91139099999-0 88150.91700799999-1', '3.3142014956882235', '-22.32397147411286', '-302.4381576835125', '20.0', '15.0', '-20.0', '0.05812076850806207', '0.02427722241941762', '0.9980143249170481', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14925633872709826', '0.2345754008162153', '0.9605711460801297']\n",
      "[88150.92678, '5', '0.8833800162178242', '0.49148149130484464', '0.3959650494113467', '88150.923423-0 88150.92752699999-1', '3.311363663876099', '-22.74809848858154', '-303.6919905798173', '20.0', '15.0', '-20.0', '0.05787439261660966', '0.026257329940315228', '0.9979785104418155', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14860165931375657', '0.2343942415664973', '0.9607168606669007']\n",
      "[88150.93678, '5', '0.9976716979910984', '0.49109736711588986', '0.3962520166514687', '88150.93622799999-0 88150.936133-1', '3.4655296294769027', '-22.717123650092198', '-304.1173390246404', '20.0', '15.0', '-20.0', '0.05725573600184664', '0.02556757341574248', '0.9980321036342059', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14890087750610775', '0.23447839734970133', '0.9606499934181163']\n",
      "[88150.94678, '6', '0.9147837667828747', '0.4913582478042822', '0.3960199736965523', '88150.94368499999-0 88150.947523-1', '3.374399071467657', '-22.838474090435426', '-305.05957760399974', '20.0', '15.0', '-20.0', '0.05738168673496691', '0.025708572282394638', '0.9980212479394674', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14811747558506844', '0.23433106058094225', '0.9608070396668197']\n",
      "[88150.95678, '6', '0.8788892496462954', '0.49133863500720726', '0.39775552495347044', '88150.955483-0 88150.95548399999-1', '3.3662896201724912', '-22.352572589595262', '-303.6373329086299', '20.0', '15.0', '-20.0', '0.057698371646870764', '0.023702105049595353', '0.9980526579923118', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1488208667257802', '0.23427704473976166', '0.9607115154587205']\n",
      "[88150.96678, '6', '0.8391189191753998', '0.49111486313443464', '0.3973271674120443', '88150.963521-0 88150.967437-1', '3.4685423579279355', '-22.545556050882794', '-304.9810889951628', '20.0', '15.0', '-20.0', '0.057074997183387884', '0.023794625042328963', '0.9980862991323006', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1484760207186315', '0.23430510114919034', '0.9607580292909492']\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "import numpy as np\n",
    "import datetime\n",
    "filelist = []\n",
    "filelists = []#存储数值求取sigma，mean\n",
    "hz=[]#100hz采样率重置\n",
    "restart=[]#记录前一个lists来\n",
    "times=0#基准时间\n",
    "cot=1#记录前20输出\n",
    "minx=114514.0\n",
    "maxx=0.0\n",
    "confidence=0#记录confidence符合的条数\n",
    "sigma=0#记录sigma符合的条数\n",
    "#通过处理每一行数据求sigma，mean，记录采样率100hz时的数据\n",
    "with open('gaze.csv', 'r', newline='') as file_in:\n",
    "        reader = csv.reader(file_in)#初始化读入\n",
    "        next(reader)\n",
    "        for lists in reader:\n",
    "            filelist.append(float(lists[3]))#x，y数据放在列表之中，为了求对应的sigma和mean\n",
    "            filelists.append(float(lists[4]))\n",
    "            minx=min(minx,float(lists[0]))#求时间戳的最大最小值，用来求采样率\n",
    "            maxx=max(maxx,float(lists[0]))\n",
    "            times=float(lists[0])#初始化基准时间用于更新数据\n",
    "            hz.append(lists)#初始化新采样率列表\n",
    "            break\n",
    "        for lists in reader:#读取文件中的每行\n",
    "            filelist.append(float(lists[3]))#x，y数据放在列表之中\n",
    "            filelists.append(float(lists[4]))\n",
    "            minx=min(minx,float(lists[0]))#求时间戳的最大最小值，用来求采样率\n",
    "            maxx=max(maxx,float(lists[0]))\n",
    "            if cot<=19:#求前20条100hz采样率下的数据\n",
    "                if float(lists[0])<times+cot*0.01:\n",
    "                    restart=lists\n",
    "                elif float(lists[0])==times+cot*0.01:\n",
    "                    hz.append(lists)\n",
    "                    cot+=1\n",
    "                else:\n",
    "                    restart[0]=times+cot*0.01\n",
    "                    hz.append(restart)\n",
    "                    cot+=1\n",
    "        sigma1 = np.std(filelist, ddof=1)#通过x，y数据列表计算sigma，mean\n",
    "        mean1 = np.mean(filelist)\n",
    "        sigma2 = np.std(filelists, ddof=1)\n",
    "        mean2 = np.mean(filelists)\n",
    "with open('gaze.csv', 'r', newline='') as file_in:\n",
    "    with open('result.csv', 'w') as file_out:#重置读入\n",
    "        reader=csv.reader(file_in)\n",
    "        writer = csv.writer(file_out)#初始化读入输出\n",
    "        header = next(reader)#第一行是表头\n",
    "        writer.writerow(header)#第一行是表头直接输出\n",
    "        #判断该行数据是否符合条件，符合条件计数器加一并输出该行，否则不输出\n",
    "        for lists in reader:\n",
    "            if float(lists[2]) >= 0.9:\n",
    "                confidence+=1\n",
    "            if mean1 + 3 * sigma1 >= float(lists[3]) >= mean1 - 3 * sigma1 and mean2 + 3 * sigma2 >= float(lists[4]) >= mean2 - 3 * sigma2:\n",
    "                sigma+=1\n",
    "            if mean1 + 3 * sigma1 >= float(lists[3]) >= mean1 - 3 * sigma1 and mean2 + 3 * sigma2 >= float(lists[4]) >= mean2 - 3 * sigma2 and float(lists[2]) >= 0.9:\n",
    "                timetamp = datetime.datetime.fromtimestamp(float(lists[0]))\n",
    "                lists[0]=timetamp.strftime(\"%Y-%m-%d %H:%M:%S.%f+0000\")\n",
    "                writer.writerow(lists)#输出筛选过后的行\n",
    "print(confidence,sigma,len(filelists)/(maxx-minx),\"Hz\")#采样率直接求了\n",
    "for x in hz:\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4edcad57",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
